#ifndef ROBOTHELPER_H
#define ROBOTHELPER_H

#include "src/pubimp.h"
#include "device/robotmanager.h"
#include "src/SoftWare/UDPConnect/UDPHelper.h"
#include "tools.h"
#include <random>
#include <atomic>
#include <tuple>
//#include "algorithm/robot_move.h"

namespace ROBOT {
    /// 定义通讯的类型
    typedef enum Tag_COM_TYPE
    {
        T_UDP = 0u,
        T_TCP
    }T_COM_TYPE;
}



typedef std::tuple<double, double, double,int,bool> HandlerReal;

class RobotHelper
{
public:

    static RobotHelper* GetInstance(){
        static QMutex mutex;
        static QScopedPointer<RobotHelper> inst;
        if (Q_UNLIKELY(!inst)) {
            mutex.lock();
            if (!inst) {
                inst.reset(new RobotHelper);
            }
            mutex.unlock();
        }
        return inst.data();
    }

    bool GetOpened(){
        try{
            bool ret = _robot->getStatus() == RUNNING;

            return ret;
        }catch(...){
            qDebug()<<"......";
            return false;
        }
    }


    void SelectRobot(){
        Config config("etc/catch.info") ;
        _robot.selectRobot(static_cast<RobotType>(config.get<int>("robotType"))) ;
        //_robot.selectRobot(RobotType::KAWASAKI) ;
    }


    /// 暂停运动
    /// arg1:is pause?
    /// true:pause  ,false:run
    void PauseMotion(bool state);

    /// 急停
    void CriticalStop(bool state);

    /// 相对运动
    void RelativeMove(RobotPos,int speed = 100);

    /// abs运动
    void ABSMove(RobotPos,int speed = 100);

    /// 送丝
    void OutSilk();

    ///
    void InSilk();

    /// 连接到机器人
    void ConnectToRobot();

    /// 设置坐标系
    void SetCoordi();

    /// 设置TCP
    void SetTCP();

    ///
    void ProcessContinousPos(double px,double py,double pz,int speed,bool motionType);

    ///
    void GetError();

    ///
    void CloseRobot();

public:
    RobotPos getCurrentPos();
    int T_P_ONCE_MAX = 10;
private slots:
    void recodeBug1();
private:
    RobotHelper();
    Config _sys_config ;
    RobotManager _robot ;
    /// 配置


    bool _bWeldingOn;   /// 焊机是否已经打开了
    bool _allowMove  = false;
    std::deque<HandlerReal> _threadHandle;
    std::atomic<bool> zeroLock = ATOMIC_FLAG_INIT;

private:
    /// 私有方法，相对运动
    void _relativeMove(RobotPos,int speed = 100);
    void _absMove(RobotPos,int speed = 100);
    /// 启动UDP链接
    void StartUDP();

    /// 读取UDP配置文件
    void ReadConfig();
};

#endif // ROBOTHELPER_H
